# Selector - Simple selector texture to show current position in menu.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import gtk
import clutter

class Selector(clutter.Group):
    """
    Simple selector widget. 
    
    Selector is an animated texture that can be used to display current position 
    in menus.
    
    CLASS ATTRIBUTES:
    @attr selector  (clutter.Texture) Selector texture
    @attr glow      (clutter.Texture) Selector glow texture
    @attr timeline  (clutter.Timeline) Timeline that animates glow effect
    """
    
    # Selector size constants (One of these should be given when initializing)
    SMALL = 0
    LARGE = 1
    
    def __init__(self, size, theme):
        """
        Initialize Selector widget and start animation.
        @param size: Size of the selector. Use class constants SMALL and LARGE
        @param theme: Theme object
        """
        clutter.Group.__init__(self)
        self.animate_selector = False
        
        # Load pixel buffers
        if size == self.__class__.SMALL:
            selector_pixbuf = gtk.gdk.pixbuf_new_from_file(theme.getImage("selector_small"))
            glow_pixbuf = gtk.gdk.pixbuf_new_from_file(theme.getImage("selector_glow_small"))    
        elif size == self.__class__.LARGE:
            selector_pixbuf = gtk.gdk.pixbuf_new_from_file(theme.getImage("selector"))
            glow_pixbuf = gtk.gdk.pixbuf_new_from_file(theme.getImage("selector_glow"))
        else:
            raise Exception("Intializing Selector object failed. Unknown size!")
        
        # Set selector base texture 
        self.selector = clutter.Texture()
        self.selector.set_pixbuf(selector_pixbuf)
        self.selector.set_opacity(200)
        self.selector.show()
        self.add(self.selector)
        
        # Set selector GLOW texture
        self.glow = clutter.Texture()
        self.glow.set_pixbuf(glow_pixbuf)
        self.glow.set_opacity(0)
        self.glow.show()
        self.add(self.glow)
        
        # Animate selector (Glow effect with glow overlay texture)
        self.timeline = clutter.Timeline(150,50)
        self.timeline.set_loop(True)
        self.alpha = clutter.Alpha(self.timeline, clutter.sine_func)
        self.behaviour = clutter.BehaviourOpacity(0, 255, self.alpha)
        self.behaviour.apply(self.glow)

    def animate(self, boolean):
        """
        Animate widget when it's on screen.
        @param boolean: True if widget should be animated, otherwise False
        """
        self.animate_selector = boolean
        #self.stop_animation()

    def stop_animation(self):
        """Stop selector animation"""
        self.timeline.stop()
        self.glow.set_opacity(0)
        
    def start_animation(self):
        """Start selector animation"""
        self.timeline.start()

# Below are overridden methods. Animation is stopped when widget is hidden.

    def hide(self):
        """Overrides clutter.Actor.hide()"""
        clutter.Group.hide(self)
        if self.animate_selector:
            self.stop_animation()
        
    def show(self):
        """Overrides clutter.Actor.show()"""
        clutter.Group.show(self)
        if self.animate_selector:
            self.start_animation()
        
    def hide_all(self):
        """Overrides clutter.Actor.hide_all()"""
        clutter.Group.hide_all(self)
        if self.animate_selector:
            self.stop_animation()
        
    def show_all(self):
        """Overrides clutter.Actor.show_all()"""
        clutter.Group.show_all(self)
        if self.animate_selector:
            self.start_animation()
